#include "hsvwidget.h"
#include "ui_hsvwidget.h"
#include "hsvcolor.h"

#include <QtGui>

HSVWidget::HSVWidget(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::HSVWidget)
{
    m_ui->setupUi(this);

    QSettings settings;
    m_ui->m_hSpin->setValue(settings.value("hsv/h").value<int>());
    m_ui->m_sSpin->setValue(settings.value("hsv/s").value<int>());
    m_ui->m_vSpin->setValue(settings.value("hsv/v").value<int>());
    m_ui->m_hrSpin->setValue(settings.value("hsv/hr").value<int>());
    m_ui->m_srSpin->setValue(settings.value("hsv/sr").value<int>());
    m_ui->m_vrSpin->setValue(settings.value("hsv/vr").value<int>());

    connect(m_ui->m_hSpin, SIGNAL(valueChanged(int)), this, SLOT(updateColor()));
    connect(m_ui->m_sSpin, SIGNAL(valueChanged(int)), this, SLOT(updateColor()));
    connect(m_ui->m_vSpin, SIGNAL(valueChanged(int)), this, SLOT(updateColor()));
    connect(m_ui->m_hrSpin, SIGNAL(valueChanged(int)), this, SLOT(updateColor()));
    connect(m_ui->m_srSpin, SIGNAL(valueChanged(int)), this, SLOT(updateColor()));
    connect(m_ui->m_vrSpin, SIGNAL(valueChanged(int)), this, SLOT(updateColor()));
}

HSVWidget::~HSVWidget()
{
    delete m_ui;
}

void HSVWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void HSVWidget::updateColor()
{
    QSettings settings;

    settings.setValue("hsv/h", m_ui->m_hSpin->value());
    settings.setValue("hsv/s", m_ui->m_sSpin->value());
    settings.setValue("hsv/v", m_ui->m_vSpin->value());
    settings.setValue("hsv/hr", m_ui->m_hrSpin->value());
    settings.setValue("hsv/sr", m_ui->m_srSpin->value());
    settings.setValue("hsv/vr", m_ui->m_vrSpin->value());

    emit colorChanged(HSVColor(m_ui->m_hSpin->value(), m_ui->m_sSpin->value(), m_ui->m_vSpin->value(), m_ui->m_hrSpin->value(), m_ui->m_srSpin->value(), m_ui->m_vrSpin->value()));
}

void HSVWidget::setColor(HSVColor color, bool omitrange)
{
    m_ui->m_hSpin->setValue(color.h());
    m_ui->m_sSpin->setValue(color.s());
    m_ui->m_vSpin->setValue(color.v());

    if (!omitrange)
    {
        m_ui->m_hrSpin->setValue(color.hr());
        m_ui->m_srSpin->setValue(color.sr());
        m_ui->m_vrSpin->setValue(color.vr());
    }
}
